package com.freejava.myblog.services;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.freejava.myblog.pojo.MyUser;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenService {
    public String getToken(MyUser user) {
        String token = "";
        // Only 1 hour is valid period
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60 * 60 * 1000;
        Date end = new Date(currentTime);

        token = JWT.create().withAudience(String.valueOf(user.getId())).withIssuedAt(start).withExpiresAt(end).sign(Algorithm.HMAC256(user.getPassword() + "MText!76&sQ^"));

        return token;
    }
}
